package com.jin.tool;

import android.app.Application;
import android.content.Context;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.lang.reflect.Field;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class hook implements IXposedHookLoadPackage {
    boolean isMe = false;
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (!lpparam.packageName.equals("com.ss.android.ugc.aweme")){
            return;
        }
        XposedBridge.log("Hook抖音");
        XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                final ClassLoader classLoader = ((Context)param.args[0]).getClassLoader();

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.ui.header.a", classLoader, "b", int.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);

                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            //XposedBridge.log("Hook方法【b】");
                            //修改关注
                            //加载类
                            if (!isMe)
                                return;
                            Class c = classLoader.loadClass("com.ss.android.ugc.aweme.profile.ui.header.a");
                            //获取类中"test"成员变量
                            Field field = c.getDeclaredField("i");
                            //成员变量为private,故必须进行此操作
                            field.setAccessible(true);
                            //获取控件对象
                            TextView follow = (TextView) field.get(param.thisObject);
                            //改变TextView的值
                            follow.setText("97");
                            super.afterHookedMethod(param);
                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.ui.header.a", classLoader, "a", long.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);

                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            //XposedBridge.log("Hook方法【a】");
                            //修改赞
                            //加载类
                            if (!isMe)
                                return;
                            Class c = classLoader.loadClass("com.ss.android.ugc.aweme.profile.ui.header.a");
                            //获取类中"test"成员变量
                            Field field = c.getDeclaredField("m");
                            //成员变量为private,故必须进行此操作
                            field.setAccessible(true);
                            //获取控件对象
                            TextView follow = (TextView) field.get(param.thisObject);
                            //改变TextView的值
                            follow.setText("26");
                            super.afterHookedMethod(param);
                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.ui.header.a", classLoader, "a", int.class, new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            //XposedBridge.log("Hook方法【k】");
                            //修改粉丝
                            if (!isMe)
                                return;
                            Class c = classLoader.loadClass("com.ss.android.ugc.aweme.profile.ui.header.a");
                            Field field = c.getDeclaredField("k");
                            field.setAccessible(true);
                            TextView fensi = (TextView)field.get(param.thisObject);
                            fensi.setText("10");


                            field = c.getDeclaredField("u");
                            field.setAccessible(true);
                            TextView id = (TextView)field.get(param.thisObject);
                            XposedBridge.log(id.getText().toString());
                            id.setText("抖音号：dy0zj3ym8y6w");
                            super.afterHookedMethod(param);
                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.model.User", classLoader, "getNickname", new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            //XposedBridge.log("Hook【getNickname】");
                            if (!isMe)
                                return;
                            param.setResult("人生如梭");
                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.model.User", classLoader, "getUniqueId", new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            XposedBridge.log("getUniqueId"+param.getResult());
                            //param.setResult("dy0zj3ym8y6w");
                            String a = ""+param.getResult();
                            if (a.equals("1113464870") || a.equals("")){
                                isMe = true;
                                param.setResult("dy0zj3ym8y6w");
                            }else {
                                isMe = false;
                            }
                        }

                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.model.User",classLoader,"getUid",new XC_MethodHook(){
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            //XposedBridge.log("Hook[getUid]:"+param.getResult());

                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.ui.ProfileTabView", classLoader, "setText", String.class,new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);

                            //param.setResult("4");
                        }

                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                            if (!isMe)
                                return;
                            XposedBridge.log("Hook【setText】");
                            String a = ""+param.args[0];
                            if (a.indexOf("作品") != -1){
                                param.args[0] = "作品 11" ;
                            }
                            if (a.indexOf("收藏") != -1){
                                param.args[0] = "收藏 13" ;
                            }
                            if (a.indexOf("喜欢") != -1){
                                param.args[0] = "喜欢 258" ;
                            }
                        }
                    });

                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.userservice.CommonFollowApi", classLoader, "a", String.class, String.class, int.class, int.class, int.class, String.class, int.class, String.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                            //XposedBridge.log("Hook[a]:"+param.args[0]+"\n"+param.args[1]+"\n"+param.args[5]+"\n"+param.args[7]+"\n");
                            param.args[0]="98224237765";
                            param.args[1]="MS4wLjABAAAANb08RkwegiPz0IWvK4Z6NMer8tHN2XVsw8YIKQYVXyc";
                        }
                    });

                    final Class<?> C = XposedHelpers.findClass("com.ss.android.ugc.aweme.profile.model.User", classLoader);
                    XposedHelpers.findAndHookMethod("com.ss.android.ugc.aweme.profile.fansshake.FansShakeView", classLoader, "c",C,new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                            XposedBridge.log("Hook[FansShakeView]");
                        }

                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                        }
                    });
                }
        });
    }
}
